package team.pikachu.pikachuclient.utils;

import android.util.Log;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.SocketTimeoutException;
import java.net.URL;

/**
 * @author Alex
 * 
 */

public class HttpUtils {
    private static HttpURLConnection connection = null;

    private static HttpURLConnection getConnection(String path){
        try {
            URL url = new URL(path);
            connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("POST");
            connection.setConnectTimeout(8000);
            connection.setReadTimeout(8000);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return connection;
    }

    public static String sendPostMethod(String path){
        StringBuilder response = new StringBuilder();
        connection = getConnection(path);
        try {
            InputStream in = connection.getInputStream();
            BufferedReader reader = new BufferedReader(new InputStreamReader(in,"UTF-8"));
            String line;
            while((line=reader.readLine())!=null){
                response.append(line);
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
            return "TIMEOUT";
        } catch (SocketTimeoutException e){
            Log.e("ERROR","链接超时");
            return "TIMEOUT";
        } catch (IOException e) {
            e.printStackTrace();
            return "TIMEOUT";
        } finally {
            if(connection != null){
                connection.disconnect();
            }
        }
        return response.toString();
    }

    public static String sendJson(String uploadURL,String json){
        connection = getConnection(uploadURL);
        try {
            connection.setDoOutput(true);
            connection.setRequestProperty("Content-Type","application/json");
            connection.setRequestProperty("Charset","UTF-8");
            connection.setRequestProperty("Accept", "application/json");
            OutputStream out = connection.getOutputStream();
            BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out));
            writer.write(json);
            writer.flush();
            writer.close();
            if (connection.getResponseCode() == 200){
                InputStream in = connection.getInputStream();
                BufferedReader reader = new BufferedReader(new InputStreamReader(in));
                String flag ;
                while((flag = reader.readLine()) != null){
                    if(flag.equals("success")){
                        return "success";
                    }else if (flag.equals("fail")){
                        return "fail";
                    }else if (flag.equals("notFound")){
                        return "notFound";
                    }
                }
            }else {
                return "fail";
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(connection != null){
                connection.disconnect();
            }
        }
        return "fail";
    }
}
